package com.kdtech.analyse.news.ulinix;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.StringUtils;
import com.kdtech.analyse.AnalyseNews;

/**
 * @author kd-15
 */
public class UztajNewsAnalyse implements AnalyseNews {

	private static final String[] regex={
		"http://www.uztaj.com/([a-z]*_?)*",
		"http://www.uztaj.com/([a-z]*-?)*",
	};


	
	public boolean isDetailPage(String url) {
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();

		news.setUrl(url);
		String title=null;
		String content=null;
		String author=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);
		//title =doc.select("div#chan_newsBlk h1").text();
		title =doc.select("div.post_info h1").text();
		
		content=HtmlCleaner.getContentHtml(url,doc.select("div.post_content"));
		
		String authorStr=null;
		authorStr=doc.select("div.post_admin a[rel=author]").text();
		//authorStr=doc.select("div.post_admin").text();
		
		//author=StringUtils.substringBetween(authorStr,"ئاپتور:","/ تۈر");
		author=StringUtils.substring(authorStr,0);
		
		String year = "";
		String month = "";
		String day ="";
		Elements year_month = doc.select("div.year_month span");
		if(year_month.size() >= 2){
			year = year_month.get(0).text();
			month = year_month.get(1).text();
		}
		day = doc.select("div.day").text();
		date = DateUtils.matchDate(year + month + day);
		

		if(StringUtils.isBlank(title)){
		}
		news.setTitle(StringUtils.trimSpace(title));
		news.setContent(content);
		news.setAuthor(author);
		news.setDate(date);

		return news;
	}

	

	

	
}
